package com.xckj.ibox.presenter;

import androidx.annotation.NonNull;

import com.xckj.ibox.bean.AgreementBean;
import com.xckj.ibox.bean.BannerBean;
import com.xckj.ibox.bean.BaseBean;
import com.xckj.ibox.bean.HomeGoodsBean;
import com.xckj.ibox.bean.HomeMenuBean;
import com.xckj.ibox.bean.NoticeBean;
import com.xckj.ibox.framework.net.RetrofitManager;
import com.xckj.ibox.global.Constant;
import com.xckj.ibox.iview.IHomeView;

import java.util.List;
import java.util.Objects;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @Author: ChenYao
 * @Describe: 首页
 * @Date: 2023/6/16 00:05
 */
public class HomePresenter {
    private final IHomeView homeView;

    public HomePresenter(IHomeView homeView) {
        this.homeView = homeView;
    }

    /**
     * 获取Banner
     */
    public void getBanner() {
        Call<BaseBean<List<BannerBean>>> call = RetrofitManager.getInstance().getService().getBanner();
        call.enqueue(new Callback<BaseBean<List<BannerBean>>>() {
            @Override
            public void onResponse(@NonNull Call<BaseBean<List<BannerBean>>> call, @NonNull Response<BaseBean<List<BannerBean>>> response) {
                if (response.isSuccessful()) {
                    BaseBean<List<BannerBean>> baseBean = response.body();
                    if (Constant.SUCCESS_CODE == Objects.requireNonNull(baseBean).code) {
                        homeView.getBannerSuccess(baseBean.data);
                    } else {
                        homeView.failed(baseBean.msg);
                    }
                } else {
                    homeView.failed("请求失败");
                }
            }

            @Override
            public void onFailure(@NonNull Call<BaseBean<List<BannerBean>>> call, @NonNull Throwable t) {
                t.printStackTrace();
                homeView.failed("请求失败");
            }
        });
    }

    /**
     * 获取首页菜单
     */
    public void getHomeMenu() {
        Call<BaseBean<List<HomeMenuBean>>> call = RetrofitManager.getInstance().getService().getHomeMenu();
        call.enqueue(new Callback<BaseBean<List<HomeMenuBean>>>() {
            @Override
            public void onResponse(@NonNull Call<BaseBean<List<HomeMenuBean>>> call, @NonNull Response<BaseBean<List<HomeMenuBean>>> response) {
                if (response.isSuccessful()) {
                    BaseBean<List<HomeMenuBean>> baseBean = response.body();
                    if (Constant.SUCCESS_CODE == Objects.requireNonNull(baseBean).code) {
                        homeView.getHomeMenuSuccess(baseBean.data);
                    } else {
                        homeView.failed(baseBean.msg);
                    }
                } else {
                    homeView.failed("请求失败");
                }
            }

            @Override
            public void onFailure(@NonNull Call<BaseBean<List<HomeMenuBean>>> call, @NonNull Throwable t) {
                t.printStackTrace();
                homeView.failed("请求失败");
            }
        });
    }

    /**
     * 获取首页商品
     *
     * @param page     页码
     * @param pageSize 条目数
     * @param type     类型
     */
    public void getHomeGoods(int page, int pageSize, int type) {
        homeView.showLoading();
        Call<BaseBean<HomeGoodsBean>> call = RetrofitManager.getInstance().getService().getHomeGoods(page, pageSize, type);
        call.enqueue(new Callback<BaseBean<HomeGoodsBean>>() {
            @Override
            public void onResponse(@NonNull Call<BaseBean<HomeGoodsBean>> call, @NonNull Response<BaseBean<HomeGoodsBean>> response) {
                if (response.isSuccessful()) {
                    BaseBean<HomeGoodsBean> baseBean = response.body();
                    if (Constant.SUCCESS_CODE == Objects.requireNonNull(baseBean).code) {
                        homeView.getHomeGoodsSuccess(baseBean.data);
                    } else {
                        homeView.failed(baseBean.msg);
                    }
                } else {
                    homeView.failed("请求失败");
                }
                homeView.hideDialog();
            }

            @Override
            public void onFailure(@NonNull Call<BaseBean<HomeGoodsBean>> call, @NonNull Throwable t) {
                t.printStackTrace();
                homeView.failed("请求失败");
                homeView.hideDialog();
            }
        });
    }

    /**
     * 获取协议
     */
    public void getAgreement() {
        Call<BaseBean<AgreementBean>> call = RetrofitManager.getInstance().getService().getAgreement();
        call.enqueue(new Callback<BaseBean<AgreementBean>>() {
            @Override
            public void onResponse(@NonNull Call<BaseBean<AgreementBean>> call, @NonNull Response<BaseBean<AgreementBean>> response) {
                if (response.isSuccessful()) {
                    BaseBean<AgreementBean> baseBean = response.body();
                    if (Constant.SUCCESS_CODE == Objects.requireNonNull(baseBean).code) {
                        homeView.getAgreement(baseBean.data);
                    } else {
                        homeView.failed(baseBean.msg);
                    }
                } else {
                    homeView.failed("请求失败");
                }
            }

            @Override
            public void onFailure(@NonNull Call<BaseBean<AgreementBean>> call, @NonNull Throwable t) {
                t.printStackTrace();
                homeView.failed("请求失败");
            }
        });

    }

    /**
     * 获取公告
     */
    public void getNotice() {
        Call<BaseBean<List<NoticeBean>>> call = RetrofitManager.getInstance().getService().getNotice();
        call.enqueue(new Callback<BaseBean<List<NoticeBean>>>() {
            @Override
            public void onResponse(@NonNull Call<BaseBean<List<NoticeBean>>> call, @NonNull Response<BaseBean<List<NoticeBean>>> response) {
                if (response.isSuccessful()) {
                    BaseBean<List<NoticeBean>> baseBean = response.body();
                    if (Constant.SUCCESS_CODE == Objects.requireNonNull(baseBean).code) {
                        homeView.getNotice(baseBean.data);
                    } else {
                        homeView.failed(baseBean.msg);
                    }
                } else {
                    homeView.failed("请求失败");
                }
            }

            @Override
            public void onFailure(@NonNull Call<BaseBean<List<NoticeBean>>> call, @NonNull Throwable t) {
                t.printStackTrace();
                homeView.failed("请求失败");
            }
        });
    }
}
